<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="1000px"
            @close="handleClose"
            @confirm="handleClose"
        >
            <div style="width: 100%; height:70vh; overflow-y: auto">
                <el-form ref="formRef" :model="formData" disabled label-width="120px" style="width:600px">
                    <el-form-item label="订单号" prop="sn">
                        <el-input v-model="formData.sn" clearable placeholder="请输入" />
                    </el-form-item>
                    <el-form-item label="支付价格" prop="pay_price">
                        <el-input v-model="formData.pay_price" clearable placeholder="请输入支付价格" />
                    </el-form-item>
                    <el-form-item label="使用时间" prop="use_time">
                        <el-input v-model="formData.use_time" clearable placeholder="请输入使用时间" />
                    </el-form-item>
                    <el-form-item label="开始时间" prop="start_time">
                        <el-input v-model="formData.start_time" clearable placeholder="请输入开始时间" />
                    </el-form-item>
                    <el-form-item label="结束时间" prop="end_time">
                        <el-input v-model="formData.end_time" clearable placeholder="请输入结束时间" />
                    </el-form-item>
                    <el-form-item label="支付状态" prop="pay_status">
                        <el-tag>
                            <template v-if="formData.pay_status == 1">未支付</template>
                            <template v-if="formData.pay_status == 2">已支付</template>
                            <template v-if="formData.pay_status == 3">已取消</template>
                        </el-tag>
                    </el-form-item>
                    <el-form-item label="订单状态" prop="status">
                        <el-tag>
                            <template v-if="formData.status == 1">正常</template>
                            <template v-if="formData.status == 2">进行中</template>
                            <template v-if="formData.status == 3">已结束</template>
                            <template v-if="formData.status == 4">异常</template>
                            <template v-if="formData.status == 5">已结算</template>
                        </el-tag>
                    </el-form-item>
                    <el-form-item label="退款状态" prop="refund_status">
                        <el-tag>
                            <template v-if="formData.refund_status == 1">未发起</template>
                            <template v-if="formData.refund_status == 2">已发起</template>
                            <template v-if="formData.refund_status == 3">已退款</template>
                            <template v-if="formData.refund_status == 4">退款失败</template>
                        </el-tag>

                    </el-form-item>
                    <el-form-item label="退款发起时间" prop="refund_start_time">
                        <el-input v-model="formData.refund_start_time" clearable placeholder="请输入退款发起时间" />
                    </el-form-item>
                    <el-form-item label="退款结束时间" prop="refund_end_time">
                        <el-input v-model="formData.refund_end_time" clearable placeholder="请输入退款结束时间" />
                    </el-form-item>
                    <el-form-item label="支付类型" prop="pay_type">
                        <el-tag>
                            <template v-if="formData.pay_type == 1">余额支付</template>
                            <template v-if="formData.pay_type == 2">微信支付</template>
                            <template v-if="formData.pay_type == 3">混合支付</template>
                        </el-tag>

                    </el-form-item>
                    <el-form-item label="用户手机号" prop="user_mobile">
                        <el-input v-model="formData.user_mobile" clearable placeholder="请输入用户手机号" />
                    </el-form-item>
                    <el-form-item label="支付时间" prop="pay_time">
                        {{ formData.pay_time ? moment(formData.pay_time * 1000).format('YYYY-MM-DD HH:mm:ss') : '' }}
                    </el-form-item>
                    <el-form-item label="收入" prop="income">
                        <el-input v-model="formData.income" clearable placeholder="请输入收入" />
                    </el-form-item>
                </el-form>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" name="boomOrderEdit" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomOrderDetail } from '@/api/boom_order'
import type { PropType } from 'vue'
import moment from 'moment'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑订单表' : '详情'
})

// 表单数据
const formData = reactive({
    id: '',
    sn: '',
    device_id: '',
    user_id: '',
    agent_id: '',
    dealer_id: '',
    share_ids: '',
    price_id: '',
    pay_price: '',
    use_time: '',
    start_time: '',
    end_time: '',
    pay_status: '',
    status: '',
    refund_status: '',
    refund_start_time: '',
    refund_end_time: '',
    pay_type: '',
    place_id: '',
    user_mobile: '',
    use_virtual_balance: '',
    transaction_id: '',
    pay_time: '',
    income: ''
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomOrderDetail({
        id: row.id
    })
    setFormData(data)
}


//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
